﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.Forms;
using System.IO.Ports;
using System.Collections;
using WFNetLib;
using System.Threading;

namespace YouJiBiaoDing
{
    public partial class Form2 : Form
    {
        public CommunicationDebugForm CommunicationDebugForm;
        SerialCommunication com;
        bool bDelay;
        int Delay, SpaceTime;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            CommunicationDebugForm = new CommunicationDebugForm();
            CommunicationDebugForm.Show();
            listView1.Items.Clear();
            ListViewItem li = listView1.Items.Add("1");
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            for (int i = 1; i < 21; i++)
                cbStartDelay.Items.Add(i);
            cbStartDelay.SelectedIndex = 0;
            for (int i = 1; i < 1000; i++)
                cbSpaceTime.Items.Add(i);
            cbSpaceTime.SelectedIndex = 9;
            for (int i = 1; i < 256; i++)
                cbTargetAddr.Items.Add(i);
            cbTargetAddr.SelectedIndex = 0;
            
            li.BackColor = Color.Red;
            com = new SerialCommunication(cbCom.Text, CommunicationDebugForm);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ArrayList AddrList = new ArrayList();
            AddrList.Clear();
            for (int i = 1; i < listView1.Items.Count; i++)
            {
                AddrList.Add(listView1.Items[i].SubItems[0].Text);
            }
            Form3 f = new Form3(AddrList,Convert.ToInt32(cbTargetAddr.Text));
            f.ShowDialog();
            while (listView1.Items.Count!=1)
                listView1.Items.RemoveAt(1);
            for (int i = 0; i < f.AddrList.Count; i++)
                listView1.Items.Add(f.AddrList[i].ToString());
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (toolStripButton2.Text == "开始")
            {
                if (!com.OpenPort(cbCom.Text))
                    return;
                toolStripButton2.Text = "停止";
                cbCom.Enabled = false;
                cbSpaceTime.Enabled = false;
                cbStartDelay.Enabled = false;
                cbTargetAddr.Enabled = false;
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    while (listView1.Items[i].SubItems.Count != 1)
                        listView1.Items[i].SubItems.RemoveAt(1);
                }
                bDelay = true;
                Delay=Convert.ToInt32(cbStartDelay.Text)*60;
                SpaceTime=Convert.ToInt32(cbSpaceTime.Text)*60;
                lbTT.Text = "启动延时";
                lbTime.Text = string.Format("{0:D2}:{1:D2}", Delay / 60, Delay % 60);
                timer1.Enabled = true;
            }
            else
            {
                toolStripButton2.Text = "开始";
                com.CloseSerialPort();
                cbCom.Enabled = true;
                cbSpaceTime.Enabled = true;
                cbStartDelay.Enabled = true;
                cbTargetAddr.Enabled = true;
                timer1.Enabled = false;
                lbTT.Text = "倒计时";
                lbTime.Text = "倒计时";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (bDelay)
            {
                Delay--;
                if (Delay == 0)
                {
                    lbTT.Text = "测量倒计时";
                    bDelay = false;
                    lbTime.Text = string.Format("{0:D2}:{1:D2}", SpaceTime / 60, SpaceTime % 60);
                    timer1.Enabled = false;
                    /************************************************************************/
                    /* 获得起始数据                                                         */
                    /************************************************************************/
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        ushort needLen = 27;
                        byte[] RxList = new byte[27];
                        int retry = 100;
                        while (retry!=0)
                        {
                            Thread.Sleep(500);
                            if (com.GetRet(Convert.ToByte(listView1.Items[i].SubItems[0].Text), needLen, ref RxList))
                                break;
                            retry--;  
                        }
                        uint t1, t2, t;
                        t1 = Convert.ToUInt32(BytesOP.MakeShort(RxList[9], RxList[10]));
                        t2 = Convert.ToUInt32(BytesOP.MakeShort(RxList[21], RxList[22]));
                        t = t1 * 60 + t2;
                        listView1.Items[i].SubItems.Add(t.ToString());
                        uint p=BytesOP.MakeInt(RxList[15], RxList[16], RxList[17], RxList[18]);
                        float pp=((float)p)/100;
                        listView1.Items[i].SubItems.Add(pp.ToString("F2"));
                        Thread.Sleep(500);
                    }
                    timer1.Enabled = true;
                }
                else
                {
                    lbTime.Text = string.Format("{0:D2}:{1:D2}", Delay / 60, Delay % 60);
                }                
            }
            else
            {
                SpaceTime--;
                if (SpaceTime == 0)
                {
                    timer1.Enabled = false;
                    /************************************************************************/
                    /* 获得结束数据                                                         */
                    /************************************************************************/
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        ushort needLen = 27;
                        byte[] RxList = new byte[27];
                        int retry = 100;
                        while (retry != 0)
                        {
                            Thread.Sleep(500);
                            if (com.GetRet(Convert.ToByte(listView1.Items[i].SubItems[0].Text), needLen, ref RxList))
                                break;
                            retry--;
                        }
                        if (retry == 0)
                        {
                            MessageBox.Show("地址" + listView1.Items[i].SubItems[0].Text + "设定修正参数不成功");
                            continue;
                        }
                        Thread.Sleep(500);
                        uint t1, t2, t3, t4;
                        t1=Convert.ToUInt32(BytesOP.MakeShort(RxList[9], RxList[10]));
                        t2=Convert.ToUInt32(BytesOP.MakeShort(RxList[21], RxList[22]));
                        t3=t1*60+t2;
                        listView1.Items[i].SubItems.Add(t3.ToString());
                        uint p = BytesOP.MakeInt(RxList[15], RxList[16], RxList[17], RxList[18]);
                        float pp = ((float)p) / 100;
                        listView1.Items[i].SubItems.Add(pp.ToString("F2"));                        
                        t1 = Convert.ToUInt32(listView1.Items[i].SubItems[1].Text);
                        t2 = Convert.ToUInt32(listView1.Items[i].SubItems[3].Text);
                        t3 = t2 - t1;
                        listView1.Items[i].SubItems.Add(t3.ToString());//历时时长
                        float p1, p2, p3,p4;
                        p1 = Convert.ToSingle(listView1.Items[i].SubItems[2].Text);
                        p2 = Convert.ToSingle(listView1.Items[i].SubItems[4].Text);
                        p3 = p2 - p1;
                        listView1.Items[i].SubItems.Add(p3.ToString("F2"));//历时功耗
                        ushort step1, step2;
                        step1=BytesOP.MakeShort(RxList[23], RxList[24]);
                        listView1.Items[i].SubItems.Add(step1.ToString());//原有步长
                        if (i == 0)
                            continue;
                        listView1.Items[i].Selected = true;
                        float pt = p3 / step1;
                        p4 = Convert.ToSingle(listView1.Items[0].SubItems[6].Text);
                        t4 = Convert.ToUInt32(listView1.Items[0].SubItems[5].Text);
                        pt = p4 * t3 / p3 / t4;
                        step2 = (ushort)(pt * step1);
                        listView1.Items[i].SubItems.Add(step2.ToString());//修正步长
                        retry = 100;
                        while (retry != 0)
                        {
                            Thread.Sleep(500);
                            if (com.SetTatget(Convert.ToByte(listView1.Items[i].SubItems[0].Text), step2))
                                break;
                            retry--;
                        }
                        if (retry == 0)
                            MessageBox.Show("地址" + listView1.Items[i].SubItems[0].Text + "设定修正参数不成功");
                    }
                    MessageBox.Show("测量完成");
                    toolStripButton2.Text = "开始";
                    com.CloseSerialPort();
                    cbCom.Enabled = true;
                    cbSpaceTime.Enabled = true;
                    cbStartDelay.Enabled = true;
                    cbTargetAddr.Enabled = true;
                    lbTT.Text = "倒计时";
                    lbTime.Text = "倒计时";
                }
                else
                {
                    lbTime.Text = string.Format("{0:D2}:{1:D2}", SpaceTime / 60, SpaceTime % 60);
                }
            }
        }

        private void cbTargetAddr_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView1.Items[0].SubItems[0].Text = cbTargetAddr.Text;
        }
    }
}
